﻿using System;
using System.Collections.Generic;
using System.Linq;
using LeonardoNews.Models;

namespace LeonardoNews.Server.Models.ArticleModels
{
    public class ArticleModel
    {
        public int ID { get; set; }

        public virtual UserModel User { get; set; }

        public string Title { get; set; }

        public string Text { get; set; }

        public string ImageUrl { get; set; }

        public virtual ICollection<CommentModel> Comments { get; set; }

        public virtual ICollection<VoteModel> Votes { get; set; }

        public int VoteResult { get; set; }

        public static ArticleModel FromArticle(Article article)
        {
            return new ArticleModel()
            {
                ID = article.ID,
                Title = article.Title,
                Text = article.Text,
                ImageUrl = article.ImageUrl,
                VoteResult = article.VoteResult,
                Comments = (from c in article.Comments
                            select CommentModel.FromComment(c)).ToList(),
                User = article.User == null ? null : UserModel.FromUser(article.User),
                Votes = (from v in article.Votes
                         select VoteModel.FromVote(v)).ToList()
            };
        }
    }
}